package cn.heyige.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.heyige.backend.common.CommResponse;
import cn.heyige.backend.dto.ShareDto;
import cn.heyige.backend.facade.ShareFacade;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("api/shares")
@RequiredArgsConstructor
@Tag(name = "分享管理", description = "分享相关接口")
public class ShareController {
    
    private final ShareFacade shareFacade;
    
    /**
     * 创建分享
     */
    @PostMapping
    @Operation(summary = "创建分享", description = "创建新的分享")
    public CommResponse<ShareDto.ShareResponse> createShare(
            @Parameter(hidden = true) @RequestAttribute("userId") Integer userId,
            @Parameter(description = "创建分享请求参数") @Valid @RequestBody ShareDto.CreateShareRequest request) {
        ShareDto.ShareResponse response = shareFacade.createShare(userId, request);
        return CommResponse.ok(response);
    }
    
    /**
     * 根据分享码获取分享信息
     */
    @GetMapping("code/{shareCode}")
    @Operation(summary = "根据分享码获取分享", description = "根据分享码获取分享信息")
    public CommResponse<ShareDto.ShareResponse> getShareByCode(
            @Parameter(description = "分享码", required = true, example = "ABC123") @PathVariable String shareCode) {
        ShareDto.ShareResponse response = shareFacade.getShareByCode(shareCode);
        return CommResponse.ok(response);
    }
    
    /**
     * 查看分享并奖励配额
     */
    @PostMapping("code/{shareCode}/view")
    @Operation(summary = "查看分享", description = "查看分享并奖励配额")
    public CommResponse<ShareDto.ShareResponse> viewShare(
            @Parameter(description = "分享码", required = true, example = "ABC123") @PathVariable String shareCode,
            @Parameter(hidden = true) @RequestAttribute(value = "userId", required = false) Long userId) {
        ShareDto.ShareResponse response = shareFacade.viewShare(shareCode, userId);
        return CommResponse.ok(response);
    }
    
    /**
     * 获取用户的分享列表
     */
    @GetMapping("my")
    @Operation(summary = "获取用户分享列表", description = "分页获取用户的分享列表")
    public CommResponse<Page<ShareDto.ShareResponse>> getUserShares(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        
        Page<ShareDto.ShareResponse> shares = shareFacade.getUserShares(userId, page, size);
        return CommResponse.ok(shares);
    }
    
    /**
     * 删除分享
     */
    @DeleteMapping("{shareId}")
    @Operation(summary = "删除分享", description = "删除指定的分享")
    public CommResponse<Void> deleteShare(
            @Parameter(hidden = true) @RequestAttribute("userId") Long userId,
            @Parameter(description = "分享ID", required = true, example = "1") @PathVariable Long shareId) {
        shareFacade.deleteShare(shareId, userId);
        return CommResponse.ok();
    }
    
    /**
     * 更新分享
     */
    @PutMapping("{shareId}")
    @Operation(summary = "更新分享", description = "更新指定的分享")
    public CommResponse<ShareDto.ShareResponse> updateShare(
            @Parameter(hidden = true) @RequestAttribute("userId") Integer userId,
            @Parameter(description = "分享ID", required = true, example = "1") @PathVariable Integer shareId,
            @Parameter(description = "更新分享请求参数") @Valid @RequestBody ShareDto.UpdateShareRequest request) {
        ShareDto.ShareResponse response = shareFacade.updateShare(shareId, userId, request);
        return CommResponse.ok(response);
    }
}